package com.zrc.superframe_base.utils.transformations;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CenterInside;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.zrc.superframe_base.R;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @Author: superman
 * @CreateTime: 11/10/21
 * @Describe: 图片加载工具
 */
public class GlideLoader {

    private static GlideLoader loader;

    private static Context context;

    public void init(Context context) {
       this.context=context;
    }

    public static GlideLoader getInstance() {
        if (loader == null) {
            synchronized (GlideLoader.class) {
                if (loader == null) {
                    loader = new GlideLoader();
                }
            }
        }
        return loader;
    }

    public void displayImage(Object path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)//设置缓存策略
                .placeholder(imageView.getDrawable())
                .skipMemoryCache(false)//设置是否跳过内存缓存
                .dontAnimate()//移除所有的动画
                .centerCrop()
                //.thumbnail(0.1f)//请求给定系数的缩略图。如果缩略图比全尺寸图先加载完，就显示缩略图，否则就不显示。系数sizeMultiplier必须在(0,1)之间，可以递归调用该方法。
                .into(imageView);
    }

    public void displayImageCenterInside(Object path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(imageView.getDrawable())
                .skipMemoryCache(false)
                .dontAnimate()
                .centerInside()
                //.thumbnail(0.1f)
                .into(imageView);
    }


    /**
     * 加载完的监听
     *
     * @param path
     * @param imageView
     * @param requestListener
     */
    public void displayImage(Object path, ImageView imageView, RequestListener requestListener) {
        RequestOptions options = new RequestOptions()
                .centerInside()
                .priority(Priority.HIGH)
                .autoClone()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        if (path != null) {
            if (path.toString().contains(".gif")) {
                Glide.with(context)
                        .asGif()
                        .load(path)
                        .dontAnimate()
                        .skipMemoryCache(false)
                        .apply(options)
                        .listener(requestListener)
                        .into(imageView);
                return;
            }
        }
        Glide.with(context)
                .load(path)
                .placeholder(imageView.getDrawable())
                .apply(options)
                .dontAnimate()
                .skipMemoryCache(false)
                .thumbnail(0.1f)
                .listener(requestListener)
                .into(imageView);
    }

    public void displayImage(Object path, ImageView imageView, float thumbnail) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(imageView.getDrawable())
                .skipMemoryCache(false)
                .dontAnimate()
                .centerCrop()
                .thumbnail(thumbnail)
                .into(imageView);
    }

    public void displayImageWH(Object path, ImageView imageView, int widht, int height) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .dontAnimate()
                .skipMemoryCache(false)
                .override(widht, height)
                .centerCrop()
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayImage(Object path, ImageView imageView, @DrawableRes int errorDraw) {
        Glide.with(imageView)
                .load(path ==null || TextUtils.isEmpty(path.toString())?errorDraw:path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .error(errorDraw)
                .placeholder(errorDraw)
                .dontAnimate()
                .centerCrop()
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayImage(Object path, ImageView imageView, @DrawableRes int errorDraw, float thumbnail) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .error(errorDraw)
                .placeholder(errorDraw)
                .dontAnimate()
                .centerCrop()
                .thumbnail(thumbnail)
                .into(imageView);
    }

    public void displayImageJianJin(Object path, ImageView imageView, @DrawableRes int errorDraw) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .error(errorDraw)
                .placeholder(errorDraw)
                .dontAnimate()
                .centerCrop()
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayImage_FitXY(Object path, ImageView imageView, @DrawableRes int errorDraw) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .error(errorDraw)
                .placeholder(errorDraw)
                .thumbnail(0.8f)
                .into(imageView);
    }

    public void displayImage_FitXY(Object path, ImageView imageView, @DrawableRes int errorDraw, int roundWidth) {
        Glide.with(context)
                .load(path)
                .apply(new RequestOptions().bitmapTransform(new RoundedCornersTransformation(roundWidth, 0)).centerInside())
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .error(errorDraw)
                .placeholder(errorDraw)
                .thumbnail(0.8f)
                .into(imageView);
    }

    public void displayImage_FitXY(Object path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .apply(new RequestOptions().fitCenter())
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .skipMemoryCache(false)
                .thumbnail(1f)
                .into(imageView);
    }

    public void displayImage(Object path, ImageView imageView, RequestOptions requestOptions) {
        Glide.with(context)
                .load(path)
                .apply(requestOptions)
                .skipMemoryCache(false)
                .thumbnail(0.1f)
                .into(imageView);
    }

    //glide在其他页面加载本地资源图片后，另一个页面会受到影响，会显示本地资源图片，慎用
    public void displayRoundConnerImage(Object path, ImageView imageView, RequestOptions requestOptions, int radius) {
        Glide.with(context)
                .load(path)
                .optionalTransform(new GlideRoundTransform(imageView.getContext(), radius))
                .skipMemoryCache(false)
                .apply(requestOptions)
                .thumbnail(0.1f)
                .dontAnimate()
                .into(imageView);
    }

    public void displayRoundConnerImage(Object path, ImageView imageView, int drawableDefault, int radius, RoundedCornersTransformation.CornerType cornerType) {
        if (path==null){
            return;
        }
        if (drawableDefault == 0) {
            Glide.with(imageView)
                    .load(path)
                    //.optionalTransform(new CenterCrop(),new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterInside(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(imageView.getDrawable())
                    .error(imageView.getDrawable())
                    .thumbnail(1)
                    .into(imageView);
        } else {
            Glide.with(imageView)
                    .load(path)
                    //.optionalTransform(new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterInside(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(drawableDefault)
                    .error(drawableDefault)
                    .thumbnail(1)
                    .into(imageView);
        }

    }
    public void displayRoundConnerImage4(Object path, ImageView imageView, int drawableDefault, int radius, RoundedCornersTransformation.CornerType cornerType) {
        if (drawableDefault == 0) {
            Glide.with(imageView)
                    .load(path)
                    //.optionalTransform(new CenterCrop(),new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(imageView.getDrawable())
                    .error(imageView.getDrawable())
                    .thumbnail(0.1f)
                    .into(imageView);
        } else {
            Glide.with(imageView)
                    .load(path)
                    //.optionalTransform(new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(drawableDefault)
                    .error(drawableDefault)
                    .thumbnail(0.1f)
                    .into(imageView);
        }

    }
    public void displayRoundConnerImage2(Object path, ImageView imageView, int drawableDefault, int radius, RoundedCornersTransformation.CornerType cornerType) {
        if (drawableDefault == 0) {
            Glide.with(imageView)
                    .load(path)
                    //.optionalTransform(new CenterCrop(),new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(imageView.getDrawable())
                    .error(imageView.getDrawable())
                    //.thumbnail(0.1f)
                    .into(imageView);
        } else {
            Glide.with(imageView)
                    .load(path)
                    //.optionalTransform(new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(drawableDefault)
                    .error(drawableDefault)
                    //.thumbnail(0.1f)
                    .into(imageView);
        }

    }

    public void displayRoundConnerImage1(Object path, ImageView imageView, int drawableDefault, int radius, RoundedCornersTransformation.CornerType cornerType) {
        if (drawableDefault == 0) {
            Glide.with(context)
                    .load(path)
                    //.optionalTransform(new CenterCrop(),new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterInside(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(imageView.getDrawable())
                    .error(drawableDefault)
                    //.thumbnail(0.1f)
                    .into(imageView);
        } else {
            Glide.with(context)
                    .load(path)
                    //.optionalTransform(new RoundedCornersTransformation(DimenUtil.dp2px(radius), 0, cornerType))
                    .apply(new RequestOptions().transform(new CenterInside(), new RoundedCornersTransformation(SizeUtils.dp2px(radius), 0, cornerType)))
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(drawableDefault)
                    .error(drawableDefault)
                    //.thumbnail(0.1f)
                    .into(imageView);
        }

    }


    public void displayImageSmall(Object path, ImageView imageView, int drawableDefault) {
        Glide.with(context)
                .load(path)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(drawableDefault)
                .error(drawableDefault)
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayImage(Object path, ImageView imageView, RequestOptions requestOptions, @DrawableRes int error) {
        Glide.with(context)
                .load(path)
                .error(error).dontAnimate()
                .skipMemoryCache(false)
                .apply(requestOptions)
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayGifImage(Object path, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

        Glide.with(context)
                .load(path)
                .apply(options)
                .skipMemoryCache(false)
                .into(imageView);
    }

    public void loadFixWHGif(Object path, ImageView iv) {
        Glide.with(iv)
                .load(path)
                .into(iv);
    }

    public void displayNOScaleTypeImage(Object path, ImageView imageView) {
        Glide.with(imageView.getContext())
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .apply(RequestOptions.circleCropTransform())
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayCircleImage(Object path, ImageView imageView) {
        Glide.with(imageView.getContext())
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .centerCrop()
                .apply(RequestOptions.circleCropTransform())
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayRoundImage(Object path, ImageView imageView, @DrawableRes int errorDraw, int roundWidth) {
        Glide.with(imageView.getContext())
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .error(errorDraw)
                .placeholder(imageView.getDrawable())
                .skipMemoryCache(false)
                .dontAnimate()
                .centerCrop()
                .apply(RequestOptions.bitmapTransform(new GlideRoundTransform(imageView.getContext(), roundWidth)))
                .thumbnail(0.8f)
                .into(imageView);
    }

    public void displayCircleImage(Object path, ImageView imageView, int defaultImg) {
        if (imageView == null) {
            return;
        }
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .placeholder(defaultImg)
                .error(defaultImg)
                .dontAnimate()
                .centerCrop()
                .apply(RequestOptions.circleCropTransform())
                .thumbnail(0.1f)
                .into(imageView);
    }


    public void displayRingCircleImage(Object path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .centerCrop()
                .optionalTransform(new RingCircleTransformation(imageView.getContext(), 2))
                .thumbnail(0.1f)
                .into(imageView);
    }

    /**
     * 带圆环默认值的图片
     *
     * @param path
     * @param imageView
     * @param drawableDefault
     */
    public void displayRingCircleImageDefault(Object path, ImageView imageView, int drawableDefault) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .dontAnimate()
                .skipMemoryCache(false)
                .centerCrop()
                .fallback(drawableDefault)
                .error(drawableDefault)
                .optionalTransform(new GlideCircleTransform(imageView.getContext(), 2))
                .thumbnail(1)
                .into(imageView);
    }

    /**
     * 带圆环默认值的图片
     *
     * @param path
     * @param imageView
     * @param drawableDefault
     */
    public void displayRingCircleImageDefault(Object path, ImageView imageView, int drawableDefault, int widht, int height) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .fallback(drawableDefault)
                .error(drawableDefault)
                .dontAnimate()
                .optionalTransform(new RingCircleTransformation(imageView.getContext(), 2, R.color._xpopup_white_color))
                .fitCenter()
                .thumbnail(0.1f)
                .into(imageView);
    }


    public void displayRingCircleImage(Object path, ImageView imageView, int ringWidth) {
        Glide.with(context)
                .load(path)
                .centerCrop()
                .dontAnimate()
                .optionalTransform(new GlideCircleTransform(imageView.getContext(), ringWidth))
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .thumbnail(0.1f)
                .into(imageView);
    }

    public void displayRingCircleImageDefault(Object path, ImageView imageView, int ringWidth, int drawableDefault) {
        Glide.with(context)
                .load(path)
                .centerCrop()
                .dontAnimate()
                .error(drawableDefault)
                .fallback(drawableDefault)
                .optionalTransform(new GlideCircleTransform(imageView.getContext(), ringWidth))
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .skipMemoryCache(false)
                .thumbnail(1f)
                .into(imageView);
    }

    public void displayRingCircleImage(Object path, ImageView imageView, int ringWidth, @ColorRes int ringColor) {
        Glide.with(context)
                .load(path)
                .centerCrop()
                .dontAnimate()
                .optionalTransform(new GlideCircleTransform(imageView.getContext(), ringWidth, ringColor))
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .thumbnail(0.1f)
                .into(imageView);
    }


    public void displayRingCircleImage(Object path, ImageView imageView, int ringWidth, @ColorRes int ringColor, int defaultDrawable) {
        Glide.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(defaultDrawable)
                .error(defaultDrawable)
                .dontAnimate()
                .optionalTransform(new GlideCircleTransform(imageView.getContext(), ringWidth, ringColor))
                .centerCrop()
                .thumbnail(0.1f)
                .into(imageView);
    }

    /**
     * 高斯模糊显示图片
     *
     * @param path            图片路径
     * @param imageView
     * @param blurValue       模糊值
     * @param defaultDrawable 默认图片
     */
    public void displayImageBlur(Object path, ImageView imageView, int blurValue, int defaultDrawable) {
        Glide.with(imageView.getContext())
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(defaultDrawable)
                //.error(defaultDrawable)
                .dontAnimate()
                .optionalTransform(new BlurTransformation(blurValue))
                .centerCrop()
                .into(imageView);
    }

    /**
     * 获取bitmap
     *
     * @param imgPath
     * @param app_logo
     * @return
     */
    public Bitmap displayBitmap(String imgPath,@DrawableRes int app_logo) {
        final Bitmap[] bitmap = new Bitmap[1];
        Glide.with(context)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .load(imgPath)
                .error(app_logo)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        LogUtils.i("httpCode" + resource);
                        bitmap[0] = resource;
                    }
                });
        return bitmap[0];
    }
}